package cn.edu.scau.dormitory_management_system.exception;

import cn.edu.scau.dormitory_management_system.exception.pojo.ExceptionStatus;
import cn.edu.scau.dormitory_management_system.exception.pojo.MyException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @author djc
 * @date 2021/8/6
 * @time 13:01
 */

@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 自定义异常
     * @param ex NotFoundTableNameException
     */
    @ExceptionHandler(MyException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public ExceptionStatus notFoundTableName(MyException ex) {
        logger.error("异常信息： {}", ex.getMessage());
        return new ExceptionStatus(ex.getCode(), ex.getMessage());
    }
}